package com.uhylab.uhuvinetest.base;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.uhylab.uhuvinetest.UHApplication;

import java.lang.ref.WeakReference;

/**
 * Created by xyb on 15/12/3 at UHylab
 *
 * @version 1.0
 */
public abstract class UHBaseActivity extends AppCompatActivity implements UHInitActivity {
    /**
     * 日志输出标志
     **/
    protected final String TAG = this.getClass().getSimpleName();
    /**
     * 当前Activity渲染的视图View
     **/
    private View mContextView = null;

    /**
     * 当前Activity的弱引用，防止内存泄露
     **/
    private static WeakReference<Activity> context = null;

    private UHApplication uhApplication;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, TAG + "onCreate");
        mContextView = LayoutInflater.from(this).inflate(bindLayout(), null);
        setContentView(mContextView);
        uhApplication = new UHApplication();
        context = new WeakReference<Activity>(this);
        uhApplication.pushTask(context);
        /**初始化试图*/
        initView(mContextView);

        /**业务操作*/
        doBusiness(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        resume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        destroy();
        uhApplication.removeTask(context);
    }

    /**
     * 获取当前Activity
     *
     * @return
     */
    public static Activity getContext() {
        if (null != context)
            return context.get();
        else
            return null;
    }
}
